import { computed, ComputedRef, ref, Ref, watch } from "vue";
import { baseModes, modes, PatrolingInfo } from "../shared";
import { patrolingSetMode } from "@/api/cruise.js";

/*
 * @Author: ReinerLau lk850593913@gmail.com
 * @Date: 2023-01-31 15:52:12
 * @LastEditors: ReinerLau lk850593913@gmail.com
 * @LastEditTime: 2023-02-03 16:03:25
 * @FilePath: \v2.0.0\src\views\patroling\composables\useModeControl.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
interface Options {
  patrolingInfo: Ref<PatrolingInfo>;
  gear: Ref<boolean>;
  caritem: ComputedRef<string>;
}

export default (options: Options) => {
  const { patrolingInfo, gear, caritem } = options;
  // 当前模式
  const mode = computed(() => {
    return patrolingInfo.value.customMode;
  });
  // 控制模式对应的图标
  const modeIconMap = new Map([
    [modes.AUTO, "mode_a"],
    [modes.MANUAL, "mode_m"],
    [modes.STOP, "mode_s"],
  ]);
  // 当前模式显示的图标
  const currentModeIcon = computed(() => {
    return modeIconMap.get(mode.value) || "mode_n";
  });

  // 当前模式文字提示
  const currentModeText = computed(() => {
    let text = "";
    if (mode.value === modes.AUTO) {
      text = "自主模式";
    } else if (mode.value === modes.MANUAL) {
      text = "手动模式：";
      if (gear.value) {
        text += "前进";
      } else {
        text += "后退";
      }
    } else if (mode.value === modes.STOP) {
      text = "停止模式";
    } else {
      text = "未知";
    }
    return text;
  });
  // 是否显示模式的下拉选择框
  const modeSelectVisible = ref(false);
  // 所有可选模式
  const modeList = [
    {
      icon: modeIconMap.get(modes.AUTO),
      value: modes.AUTO,
      title: "自主模式",
    },
    {
      icon: modeIconMap.get(modes.MANUAL),
      value: modes.MANUAL,
      title: "手动模式",
    },
  ];
  // 当前选择模式
  function currentModeSelect(type) {
    return mode.value === type;
  }
  /**
   * @description: 切换模式
   * @param {*} val
   * @return {*}
   */
  function changeMode(val) {
    modeSelectVisible.value = false;
    switch (val) {
      case modes.AUTO:
        setMode("AUTO");
        break;
      case modes.MANUAL:
        setMode("MANUAL");
        break;
      case modes.STOP:
        setMode("STOP");
        break;
    }
  }
  /**
   * @description: 切换模式
   * @return {*}
   */
  async function setMode(type: string) {
    const data = { baseMode: baseModes[type], customMode: modes[type] };
    await patrolingSetMode(caritem.value, data);
  }

  const baseMode = computed(() => {
    return patrolingInfo.value.baseMode;
  });
  const baseModeText = computed(() => {
    switch (baseMode.value) {
      case baseModes.AUTO:
        return "解锁";
      case baseModes.MANUAL:
        return "解锁";
      case baseModes.STOP:
        return "锁定";
      default:
        return "未知";
    }
  });

  const stopMode = ref(false);
  watch(stopMode, controlStopMode);
  function toggleStopMode() {
    stopMode.value = !stopMode.value;
  }
  function controlStopMode() {
    if (stopMode.value) {
      setMode("STOP");
    } else {
      setMode("AUTO");
    }
  }

  return {
    mode,
    currentModeIcon,
    currentModeText,
    modeSelectVisible,
    modeList,
    currentModeSelect,
    changeMode,
    setMode,
    baseModeText,
    toggleStopMode,
    stopMode,
  };
};
